package cn.kgc.user.service;

import cn.kgc.common.entity.Author;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * Created by teacher ZHANG on 2023/2/7
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private RestTemplate restTemplate;
    @Value("${rest.login.url}")
    private String loginUrl;
    @Value("${rest.phone.url}")
    private String phoneUrl;

    @Override
    public Boolean saveAuthor(String token, Author author) {
        return restTemplate.postForObject(loginUrl + "?token=" + token, author, Boolean.class);
    }

    @Override
    public Author findAuthor(String token) {
        return restTemplate.getForObject(loginUrl + "/" + token, Author.class);
    }

    @Override
    public Boolean savePhone(String phone, String code) {
        String url = phoneUrl + "?phone=" + phone + "&code=" + code;
        return restTemplate.postForObject(url, null, Boolean.class);
    }

    @Override
    public String getPhone(String phone) {
        return restTemplate.getForObject(phoneUrl + "?phone=" + phone, String.class);
    }
}
